package com.caowj.composedemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.view.WindowCompat
import com.caowj.composedemo.activity.IndexActivity
import com.caowj.composedemo.composable.MainCompose2
import com.caowj.composedemo.ui.theme.ComposeDemoTheme
import com.google.accompanist.insets.ProvideWindowInsets
import com.google.accompanist.pager.ExperimentalPagerApi

// https://developer.android.google.cn/jetpack/compose/tutorial?authuser=0
class MainActivity : ComponentActivity() {

    @OptIn(ExperimentalPagerApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeDemoTheme {
                Surface(color = MaterialTheme.colors.background) {
                    //设置为沉浸式状态栏
                    WindowCompat.setDecorFitsSystemWindows(window, false)

                    //可以获取状态栏高度
                    ProvideWindowInsets {
                        // 写法一：一个Activity，多个Compose，使用Navigation
//                        MainCompose(onFinish = { finish() })
                    }

                    // 写法二：多个Activity
                    MainCompose2(this)
                }
            }
        }


        // 下面是PlayAndroid的写法
//        setContent {
//            val themeState = themeTypeState.value
//            //设置为沉浸式状态栏
//            WindowCompat.setDecorFitsSystemWindows(window, false)
//
//            //主题包裹
//            CustomThemeManager.WanAndroidTheme(themeState) {
//                //可以获取状态栏高度
//                ProvideWindowInsets {
//                    //主界面
//                    MainCompose(onFinish = { finish() })
//                }
//            }
//        }

    }

    @Deprecated(message = "写法废弃了")
    @Preview
    @Composable
    fun DefaultPreview() {
        ComposeDemoTheme {
            Column {
//                Text垂直居中，必须使用父容器;
//                方法一：
                Box(
                    modifier = Modifier
                        .height(100.dp)
                        .background(color = Color.Magenta),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "主页",
                        modifier = Modifier
                            .clickable {
                                startActivity(
                                    Intent(
                                        this@MainActivity,
                                        IndexActivity::class.java
                                    )
                                )
                            }
                            .wrapContentSize(align = Alignment.CenterEnd)
                            .background(color = Color.LightGray)
                            .fillMaxWidth()//宽度撑满
                            .height(50.dp),
                        color = Color.Green,
                        //文本的对齐方式
                        textAlign = TextAlign.Center,
                        fontSize = 20.sp,
                    )
                }

//                方法二：
                Column(
                    modifier = Modifier
                        .height(50.dp)
                        .background(color = Color.Yellow),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally,
                ) {
                    Text(
                        text = "Text",
                        Modifier.width(100.dp),
                        textAlign = TextAlign.Center,
                    )
                }

                Text(
                    text = "测试",
//            color = Color.Red,
                    fontSize = 16.sp,
                    fontWeight = FontWeight.Bold,
                    fontStyle = FontStyle.Normal,
                    overflow = TextOverflow.Ellipsis,
//                    textAlign = TextAlign.Center,
                    style = TextStyle(
                        color = Color.White,
                        background = Color.DarkGray
                    ),
                    modifier = Modifier
                        .width(200.dp)
                        .height(100.dp)
                        .wrapContentSize(align = Alignment.CenterEnd)
                        .clickable(onClick = {
                            Log.e("点击了全文本", "点击事件")
                        })
                        .background(color = Color.Yellow)
                )
            }

        }
    }
}


